﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using IOP.Extension.DependencyInjection;
using IOP.ISEA;
using Silk.NET.Vulkan;

namespace VkSample121;

[ModulePriority(ModulePriority.Assets)]
public class AssetsModule : VulkanModule, ISceneModule
{
    /// <summary>
    /// 
    /// </summary>
    public Scene Scene { get; set; }
    [Autowired]
    private ILogger<AssetsModule>? _Logger { get; set; }

    protected override Task Load(VulkanGraphicsManager graphicsManager)
    {
        try
        {
            var basePath = AppContext.BaseDirectory;
            var modelsPath = Path.Combine(basePath, "Assets", "Models");
            var texPath = Path.Combine(basePath, "Assets", "Textures");
            var lDevice = graphicsManager.VulkanDevice;

            graphicsManager.TryGetRenderGroup("Common", out IRenderGroup common);
            var render = graphicsManager.CreateRenderObject<RenderObject>("Ball");
            var light = graphicsManager.CreatePointLight<RenderObject>("main", new Vector3(0, 0, 5), new LightStrength(0.6f, 0.6f, 0.6f, 0.6f));
            var ballData = new BallData();
            ballData.GenBallData(9);
            var vro = graphicsManager.CreateEmptyMesh<VRO>("Ball").
                CreateVecticesBuffer(ballData.Data, (uint)ballData.VCount, lDevice, SharingMode.Exclusive);
            render.AddComponent(vro);

            var commonArchetype = ContextManager.CreateArchetype("Common", new TransformComponent());
            var lightArchetype = ContextManager.CreateArchetype<LightGroup>("Light");
            ContextManager.CreateContexts(1, commonArchetype, common, render, false);
            ContextManager.CreateContexts(1, lightArchetype, common, light, false);
        }
        catch (Exception e)
        {
            _Logger?.LogError(e.Message + "\r\n" + e.StackTrace);
        }
        return Task.CompletedTask;
    }

    protected override Task Unload(VulkanGraphicsManager manager)
    {
        return Task.CompletedTask;
    }
}
